Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse decimals and output for rational fields in protocol parameters and genesis #2992

Merged
merged 2 commits into from
Jul 31, 2021

Conversation

newhoggy
Copy link
Contributor

No description provided.

@newhoggy newhoggy changed the title Parse decimals for rational fields in protocol parameters. Parse decimals for rational fields in protocol parameters Jul 30, 2021
@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from 023a7e7 to b10f7c2 Compare July 30, 2021 04:44
@newhoggy
Copy link
Contributor Author

Output before compared to output after:

$ diff <(CARDANO_NODE_SOCKET_PATH=example/node-bft1/node.sock cardano-cli query protocol-parameters --out-file /dev/stdout --testnet-magic 42) <(CARDANO_NODE_SOCKET_PATH=example/node-bft1/node.sock $(./scripts/bin-path.sh cardano-cli) query protocol-parameters --out-file /dev/stdout --testnet-magic 42)
127,134c127,128
<         "priceSteps": {
<             "numerator": 1,
<             "denominator": 1
<         },
<         "priceMemory": {
<             "numerator": 1,
<             "denominator": 1
<         }
---
>         "priceSteps": 1,
>         "priceMemory": 1

@newhoggy
Copy link
Contributor Author

Alonzo genesis before and after:

$ diff example/shelley/genesis.alonzo.json ../genesis.alonzo.json
102,109c102,103
<         "prMem": {
<             "numerator": 1,
<             "denominator": 10
<         },
<         "prSteps": {
<             "numerator": 1,
<             "denominator": 10
<         }
---
>         "prMem": 0.1,
>         "prSteps": 0.1

@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from b4dc382 to 34b901c Compare July 30, 2021 05:40
@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from 34b901c to e641457 Compare July 30, 2021 05:44
@newhoggy newhoggy changed the title Parse decimals for rational fields in protocol parameters Parse decimals and output for rational fields in protocol parameters and genesis Jul 30, 2021
Copy link
Contributor

@dcoutts dcoutts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only need the ToJSON side of things, and the FromJSON side should already work. See details below. Can you check?

cardano-api/src/Cardano/Api/ProtocolParameters.hs Outdated Show resolved Hide resolved
Comment on lines 360 to 364
toRationalJSON :: Rational -> Aeson.Value
toRationalJSON r =
case Scientific.fromRationalRepetend (Just 5) r of
Right (s, Nothing) -> toJSON s
_ -> toJSON r
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. For whatever reason, although the Rational instance for FromJSON accepts both number and object style, the ToJSON instance never produces numbers, only object style:

instance (ToJSON a, Integral a) => ToJSON (Ratio a) where
    toJSON r = object [ "numerator"   .= numerator   r
                      , "denominator" .= denominator r
                      ]
    {-# INLINE toJSON #-}

    toEncoding r = E.pairs $
        "numerator" .= numerator r <>
        "denominator" .= denominator r
    {-# INLINE toEncoding #-}

So we do need this toRationalJSON workaround.

Perhaps that is worth proposing upstream as a PR to do the above trick for the instance provided by the library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also:

λ> encode (1 :: Rational)
"{\"numerator\":1,\"denominator\":1}"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until and if such change gets merged upstream, I'd also suggest importing toRationalJSON from Cardano.Api.Orphans instead of redefining another copy again here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hesitate mainly because I can't use it unless I export it from Cardano.Api anyway, in which case it becomes part of our API which I don't want especially because we want to push it upstream in a way that doesn't need an extra function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not realize Cardano.Api.Orphans is an exposed module. That is a very strange thing to do considering that anyone who imports Cardano.Api will get all those orphan instances into scope anyways (all instances are imported through modules transitively). I would suggest moving that module into other-modules in the cabal file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, my mistake. I thought I was using toRationalJSON from outside cardano-api, but I don't. But it still is a bit strange to export functions from a module that called Orphans. Not a hard objection though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move it to a new module Cardano.Api.Json. Seems the cleanest.

@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from e641457 to be40af9 Compare July 30, 2021 11:50
@newhoggy newhoggy requested review from dcoutts and lehins July 30, 2021 12:07
@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from be40af9 to 56485ff Compare July 30, 2021 12:25
Copy link
Contributor

@lehins lehins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few suggestions, but none of them are problematic. This PR is strictly better than it was before, in fact I very recently thought about suggesting this exact same approach for formatting Rational in Aeson, but haven't got around doing it. I am just not sure what to suggest as a default limit for repetend detection

@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from 56485ff to d07abec Compare July 30, 2021 12:51
@newhoggy newhoggy force-pushed the rational-json-improvements-for-protocol-parameters branch from d07abec to 6c978ec Compare July 30, 2021 15:11
@newhoggy
Copy link
Contributor Author

bors merge

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Jul 31, 2021

@iohk-bors iohk-bors bot merged commit b2ac378 into master Jul 31, 2021
@iohk-bors iohk-bors bot deleted the rational-json-improvements-for-protocol-parameters branch July 31, 2021 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants